package com.xbongbong.pro.message.pushset;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.message.domain.entity.PushSetEntity;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.pro.message.base.AbstractPushSet;
import com.xbongbong.pro.message.constant.PushSetConstant;
import com.xbongbong.pro.message.enums.PushObjectTypeEnum;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.model.PushSetModel;
import com.xbongbong.saas.constant.BasicConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author: 汪腾伟
 * @date: 2020/8/21
 * @desrcption: 回款计划推送设置
 * @version:1.0
 */
public class PaymentTaskPushSet extends AbstractPushSet {

    /**{
     "type": "|1|2|",				//此处用到执行人类型
     "role": {				推送的角色
     "enable": 1,
     "value": ["10",
     "30"]
     },
     "manager": {			//推送给上级主管
     "enable": 0,
     "value": ["2","5"]	//表示主管层级
     }
     }
     */
    private String pushObject;

    public String getPushObject() {
        return pushObject;
    }

    public void setPushObject(String pushObject) {
        this.pushObject = pushObject;
    }

    @Override
    public PushSetEntity toPushSetEntity(String pushObject, String pushTime, String attrEntity) {
        setPushObject(pushObject);

        PushSetEntity pushSetEntity = new PushSetEntity();

        pushSetEntity.setAddTime(addTime);
        pushSetEntity.setAttrEntity(attrEntity);
        pushSetEntity.setCorpid(corpid);
        pushSetEntity.setDel(del);
        pushSetEntity.setEnable(enable);
        pushSetEntity.setId(id);
        pushSetEntity.setPushObject(pushObject);
        pushSetEntity.setPushTime(pushTime);
        pushSetEntity.setRefEntity(refEntity);
        pushSetEntity.setSubType(subType);
        pushSetEntity.setType(type);
        pushSetEntity.setUpdateTime(updateTime);
        return pushSetEntity;
    }

    @Override
    public PushSetEntity getPushSet(String corpid, Integer subType, PushSetModel pushSetModel) {
        Integer type = PushTypeEnum.PAYMENT_TASK_PUSH.getCode();

        Map<String,Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", corpid);
        param.put("type", type);
        param.put("subType", subType);

        List<PushSetEntity> pushSetList = pushSetModel.findEntitys(param);
        PushSetEntity pushSetEntity;
        if(pushSetList.isEmpty()){
            pushSetEntity = new PushSetEntity();
            pushSetEntity.setType(type);
            pushSetEntity.setSubType(subType);
            pushSetEntity.setEnable(BasicConstant.ONE);
            JSONObject pushObject = new JSONObject();
            if (Objects.equals(PushTypeEnum.PAYMENT_TASK_ADD_PUSH.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.PAYMENT_TASK_ADD_PUSH.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.PAYMENT_TASK_ADD));
                //type
                JSONArray typeArray = new JSONArray();
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("name", PushObjectTypeEnum.EXECUTOR.getName());
                jsonObject.put("enable", BasicConstant.ONE);
                jsonObject.put("alias", PushObjectTypeEnum.EXECUTOR.getAlias());
                typeArray.add(jsonObject);
                pushObject.put("type", typeArray);
                //role
                JSONObject roleObject = new JSONObject();
                roleObject.put("enable", BasicConstant.ZERO);
                roleObject.put("value", new ArrayList<>());
                pushObject.put("role", roleObject);
            }
            pushSetEntity.setPushObject(pushObject.toJSONString());
        } else {
            pushSetEntity = pushSetList.get(0);
            if (Objects.equals(PushTypeEnum.PAYMENT_TASK_ADD_PUSH.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.PAYMENT_TASK_ADD_PUSH.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.PAYMENT_TASK_ADD));
            }
        }
        return pushSetEntity;
    }

    @Override
    public PushSetEntity getPushSet(String corpid, String attr, PushSetModel pushSetModel) {
        return null;
    }

    @Override
    public List<PushSetEntity> getPushSet(String corpid, List<String> attrList, PushSetModel pushSetModel, Long templateId) {
        return null;
    }

    @Override
    public JSONObject getTimeJson(JSONObject jsonObject) {
        return null;
    }
}
